#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
sem_t P_sem, V_sem;
int rand_num=0;

void *P_test(void *arg){
    for (int i = 0; i < 5; i++)
    {
        sem_wait(&P_sem);
        rand_num = rand();
        printf("P: %d\n", rand_num);
        sem_post(&V_sem);
    }
}


void *V_test(void *arg){
    for (int i = 0; i < 5; i++)
    {
        sem_wait(&V_sem);
        rand_num = rand();
        printf("V: %d\n", rand_num);
        sem_post(&P_sem);
    }
}

int main(int argc, char const *argv[])
{
    
    pthread_t P_thread, V_thread;

    sem_init(&P_sem, 0, 1);
    sem_init(&V_sem, 0, 0);


    pthread_create(&P_thread, NULL, P_test, NULL);
    pthread_create(&V_thread, NULL, V_test, NULL);
    pthread_join(P_thread, NULL);
    pthread_join(V_thread, NULL);


    sem_destroy(&P_sem);
    sem_destroy(&V_sem);

    return 0;
}
